﻿$(function () {
    var Page_Size = 5;
    $("#Sdate").live("focus", txtSdateConfig);//开始日期控件配置
    $("#Edate").live("focus", txtEdateConfig);//截止日期控件配置
    $("#expandTbSum tbody tr").live("click", function () {
        window.open("FinanceAdmin/ReferrerLogDetail?UserId=" + $(this).attr("data-userId"), "_blank");
    });
    $("#btnChange").click(btnChangeClick);
    $("#btnFilterRefferLog").click(btnFilterRefferLogClick);
    
    var hiddendpointTotal = $("#hiddendpointTotal").val();//总获得
    var hiddendusedPointTotal = $("#hiddendusedPointTotal").val();//已兑换
    $("#TotalPoint").text(hiddendpointTotal);
    $("#ChangedPoint").text(hiddendusedPointTotal);

    $("#btnFilterRefferLog").click();

    function btnChangeClick() {
        var logStr = "";
        var check = true;
        var $inputs = $(".textChangePoint");
        for (var i = 0; i < $inputs.length; i++) {
            var $unit = $($inputs[i]);
            var v = $.trim($unit.val());
            var logId = $unit.attr("data-logId");
            var unUsedPoint = parseFloat($unit.attr("data-Max"));
            if (v != "") {
                if (isNaN(v) || parseFloat(v) < 0) {
                    alert("请输入一个正数");
                    $unit.focus();
                    return;
                }
                else {
                    if (parseFloat(v) > unUsedPoint) {
                        alert("填写的数字过大，最多可以兑换" + unUsedPoint + "分");
                    }
                    else {
                        //alert(logId + "-->" + v);
                        logStr += logId + "@" + v+"$";
                    }
                }
            }
        };
        if (logStr != "") {
            var userId = $("#ReferrerId").text();
            $.getJSON("../FinanceAdmin/HandlerPoint", { UserId: userId, logStr: logStr }, function (result) {
                if (result.Result.IsSuccess) {
                    art.dialog.notice({
                        width: 120,
                        content: '兑换成功',
                        icon: 'succeed',
                        time: 3
                    });
                }
                else {
                    art.dialog({
                        content: result.Result.ErrMessage
                    });
                }
            });
        }
    }
    function txtSdateConfig() {
        var endValue = new Date();
        WdatePicker({
            minDate: '1900-01-01', maxDate: endValue,
            onpicked: function () {

            }
        });
    }

    function txtEdateConfig() {
        var endValue = new Date();
        WdatePicker({
            minDate: '1900-01-01',
            onpicked: function () {

            }
        })
    };

    function btnFilterRefferLogClick()
    {
        var userId = $("#ReferrerId").val();
        var userName = $("#Referrer").val();
        var referrerStartDate = $("#Sdate").val();
        var referrerEndDate = $("#Edate").val();
        var data = { UserId: userId, UserName: userName, ReferrerStartDate: referrerStartDate, ReferrerEndDate: referrerEndDate, page: 1, pageSize: Page_Size };
        GetReflogSumPage(data);
    }

    function GetReflogSumPage(f) {
        //分页控件   
        var count = GetReferrerLogSumListCount(f);
        $("#pp").pagination(count, {
            link_to: "javascript:void(0)",
            num_edge_entries: 2, //边缘页数
            num_display_entries: 7, //主体页数
            callback: onSelectPageFun,
            items_per_page: Page_Size, //每页显示项
            prev_text: "&laquo;",
            next_text: "&raquo;"
        });

        //响应事件
        function onSelectPageFun(page) {
            var $img = getLoadPic();
            var $tbody = $("#expandTbSum tbody");
            f.page = page + 1;
            var url = "../FinanceAdmin/GetReferrerLogSumList";
            $.ajax({
                url: url,
                data: f,
                dataType: "json",
                beforeSend: function () {
                    $tbody.empty();
                    $tbody.append($img);
                },
                type: "post",
                success: function (result) {
                    renderLogSumFun(result);//响应事件
                },
                async: true,
                complete: function () {
                    $img.remove();
                }
            });
        }
        //END
    };

    //数据渲染
    function renderLogSumFun(result) {
        var total = 0;
        var $container = $("#expandTbSum tbody");
        $container.empty();
        var list = result;
        $.each(list, function (k, v) {
            total += parseInt(v.TotalPoint);
        });
        var source = $("#ReflogSum-template").html();
        var template = Handlebars.compile(source);
        var html = template({ joins: list });
        $container.append(html);
        $("#spanTotal").html(total);
    };
    function GetReferrerLogSumListCount(f) {
        var count = 0;
        $.ajax({
            url: "../FinanceAdmin/GetReferrerLogSumListCount",
            data: f,
            success: function (res) {
                count = res;
            },
            async: false
        });
        return count;
    }


});